package com.jumper.property.comparer.core.diff.getter;

import com.jumper.property.comparer.core.bean.ObjectKeySetting;
import com.jumper.property.comparer.core.diff.Element;
import com.jumper.property.comparer.core.util.StringUtils;
import com.jumper.property.comparer.core.util.ValueUtil;
import com.jumper.property.comparer.core.diff.value.SubjectDiff;

import java.util.stream.Collectors;

public class ObjectDisplayNameDiffGetter extends AbstractDiffGetter<ObjectKeySetting> {

    @Override
    public void visit(Element<ObjectKeySetting> element) {
        ObjectKeySetting setting = element.getSetting();
        Object afterValue = element.getAfterValue();
        if (afterValue == null) {
            return;
        }

        String displayNames = getDisplayNames(setting, afterValue);
        if (!StringUtils.isEmpty(displayNames)) {
            SubjectDiff diff = new SubjectDiff(displayNames);
            addDiffs(diff.getDiffValues());
        }
    }

    private String getDisplayNames(ObjectKeySetting setting, Object afterValue) {
        return setting.getDisplayFields()
                .stream()
                .map(e -> ValueUtil.getStrValue(afterValue, e))
                .collect(Collectors.joining(" "));
    }
}
